﻿Imports System.Net.Sockets

Public Class SocketAsyncEventArgsPool
    Private m_pool As Stack(Of SocketAsyncEventArgs)

    Public Sub New(ByVal capacity As Integer)
        m_pool = New Stack(Of SocketAsyncEventArgs)(capacity)
    End Sub
    ''' <summary>
    ''' 将一个 EventArgs 放到缓冲区
    ''' </summary>
    ''' <param name="item"></param>
    Public Sub Push(ByVal item As SocketAsyncEventArgs)
        If item Is Nothing Then
            Throw New ArgumentNullException("Items added to a SocketAsyncEventArgsPool cannot be null")
        End If

        SyncLock m_pool
            m_pool.Push(item)
        End SyncLock
    End Sub
    ''' <summary>
    ''' 从缓冲区中读取一个EventArgs
    ''' </summary>
    ''' <returns></returns>
    Public Function Pop() As SocketAsyncEventArgs
        SyncLock m_pool
            If m_pool.Count > 0 Then
                Return m_pool.Pop()
            End If

        End SyncLock
    End Function

    Public ReadOnly Property Count As Integer
        Get
            Return m_pool.Count
        End Get
    End Property
End Class
